package com.zx.Interceptor;

import com.zx.common.ResponseCode;
import com.zx.entity.User;
import com.zx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("loginUser");
        //非管理员无法访问后台
        if(StringUtils.isEmpty(username)){
            //没有登录返回登录选择界面
            request.setAttribute("msg",ResponseCode.LOGIN_ERROR.getDesc());
            request.getRequestDispatcher("/user/login").forward(request,response);
            return false;
        }
        return true;
    }
}